package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.CardDetail;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.CardDetailView;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/23 0023.
 */

public class CardDetailPersenter extends BasePresent<CardDetailView> {

    Call<CardDetail> cardDetailCall;

    public void getCardDetail(String requestStr) {

        view.showProgress();
        final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);
        RetrofitSerives requestSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        cardDetailCall = requestSerives.getCardDetial(body);

        cardDetailCall.enqueue(new Callback<CardDetail>() {
            @Override
            public void onResponse(Call<CardDetail> call, Response<CardDetail> response) {
                if (response.isSuccessful()) {
                    if (response.body().getResult() != null) {
                        view.setCardDetail(response.body());
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<CardDetail> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });

    }

    @Override
    public void stopRequest() {

    }
}
